package com.testshiro.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.web.BaseController;
import com.base.util.ObjMapUtil;
import com.base.util.PageUtil;
import com.base.criteria.Page;
import com.base.web.JModelAndView;
import com.base.model.ResultData;
import com.base.model.RetMsg;
import com.testshiro.entity.SysAutho;
import com.testshiro.model.ListBean;
import com.testshiro.service.SysAuthoService;
import com.testshiro.service.SysRoleService;
import com.testshiro.utils.CommonValue;
/**
 * 
 * <br>
 * <b>功能：</b>SysAuthoController<br>
 *   <br>
 */ 
@Controller
@RequestMapping( value = "/data/sysautho" )
public class SysAuthoController{
	
	private final static Logger log= Logger.getLogger(SysAuthoController.class);
	@Resource
	private SysAuthoService sysauthoService; 
	
	@Resource
	private SysRoleService sysroleService; 
	
	@RequestMapping( value = "create", method = RequestMethod.POST )
    @ResponseBody
    public RetMsg create( SysAutho newObj )throws Exception {
    	RetMsg retMsg = new RetMsg();
    	
        try{
         	sysauthoService.add( newObj );
        } catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			retMsg.setCode(0);
			retMsg.setMsg("出错了！请联系管理员");
		}
       
        return retMsg;
    }
    
    @ResponseBody
    @RequestMapping( value = "update", method = RequestMethod.POST )
    public RetMsg update( SysAutho updateObj) throws Exception{
	 	RetMsg retMsg = new RetMsg();
	 	
        try{
         	sysauthoService.updateBySelective( updateObj );
        } catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			retMsg.setCode(0);
			retMsg.setMsg("出错了！请联系管理员");
		}
       
        return retMsg;
    }
    
    @ResponseBody
    @RequestMapping( value = "delete", method = RequestMethod.POST )
    public RetMsg deleteByid( String id ) throws Exception{
       RetMsg retMsg = new RetMsg();
       
       sysauthoService.delete( id );
        return retMsg;
    }
    
 
    @ResponseBody
    @RequestMapping( value = "readById", method = RequestMethod.GET )
    public SysAutho readById( Long seqId ) throws Exception {
        SysAutho result =sysauthoService.queryById( seqId );
        return result;
    }
    
    @ResponseBody
    @RequestMapping( value = "count", method = RequestMethod.GET )
    public Integer count( Integer page,Integer rows,SysAutho queryObj ) throws Exception {
		Page pageObj = new Page();
		pageObj.setCurrentPage(page);
	    pageObj.setPageSize(rows);
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    pageObj.setCondition(tmp);
        Integer ret =sysauthoService.queryByCount( pageObj );
        return ret;
    }
    
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping( value = "readList", method = RequestMethod.GET )
    public List<SysAutho> readList( SysAutho queryObj ) throws Exception {
        List<SysAutho> ret = null;
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    Page page = new Page();
	    page.clear();
	    page.setCondition(tmp);
	    
        ret =sysauthoService.queryByList( page );
        return ret;
    }
    
   
    @ResponseBody
    @RequestMapping( value = "readAndCount", method = RequestMethod.GET )
    public ResultData readAndCount(Integer page,Integer rows,SysAutho queryObj) throws Exception {

        List<SysAutho> ret = null;
        Page pageObj = new Page();
		pageObj.setCurrentPage(page);
	    pageObj.setPageSize(rows);
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    pageObj.setCondition(tmp);
	    
        ret =sysauthoService.queryForPage( pageObj );

        ResultData<SysAutho> result = new ResultData<SysAutho>();
        result.setRows(ret);
        result.setTotal(pageObj.getTotalCount());
        return result;
    }
    
    @ResponseBody
    @RequestMapping( value = "logicDel", method = RequestMethod.POST )
    public RetMsg logicDel( Integer id ) throws Exception{
       RetMsg retMsg = new RetMsg();
       
   	   SysAutho queryObj = new SysAutho();
   	   queryObj.setSeqId(id);
   	   queryObj.setIsDel(1);
   	   try{
   	   		sysauthoService.updateBySelective( queryObj );
   	   }catch (Exception e1) {
			e1.printStackTrace();
			log.info(e1.getMessage());
			retMsg.setCode(0);
			retMsg.setMsg("出错了！请联系管理员");
			return retMsg;
		}
        
       
        return retMsg;
    }
    
    
    /**
     * 分页查询根权限
     * @param page
     * @param rows
     * @param queryObj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping( value = "readRoot", method = RequestMethod.GET )
    public ResultData readRoot(Integer page,Integer rows,SysAutho queryObj) throws Exception {
    	queryObj.setParentId(CommonValue.parentIdDefault);
    	queryObj.setIsDel(CommonValue.USED);
        List<SysAutho> ret = null;
        Page pageObj = new Page();
		pageObj.setCurrentPage(page);
	    pageObj.setPageSize(rows);
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    pageObj.setCondition(tmp);
	    
        ret =sysauthoService.queryForPage( pageObj );

        ResultData<SysAutho> result = new ResultData<SysAutho>();
        result.setRows(ret);
        result.setTotal(pageObj.getTotalCount());
        return result;
    }
    
    
    /**
     * 查询根权限列表
     * @param queryObj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping( value = "readRootList", method = RequestMethod.GET )
    public List<ListBean> readRootList(SysAutho queryObj) throws Exception {
    	queryObj.setParentId(CommonValue.parentIdDefault);
    	queryObj.setIsDel(CommonValue.USED);
    	List<SysAutho> ret = null;
	    ObjMapUtil obj = new ObjMapUtil();
	    Map tmp = obj.getValueMap(queryObj);
	    Page page = new Page();
	    page.clear();
	    page.setCondition(tmp);
	    List<ListBean> list= new ArrayList<ListBean>();
        ret =sysauthoService.queryByList( page );
        for(SysAutho autho : ret){
        	ListBean listbean = new ListBean();
        	listbean.setSeqId(autho.getSeqId());
        	listbean.setText(autho.getAuthoName());
        	list.add(listbean);
        }
        return list;
    }
    
    
    /**
     * 获取前端数据结构列表
     * @param request
     * @param queryObj
     * @param roleId
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping( value = "readWebList", method = RequestMethod.GET )
    public List<ListBean> readWebList(HttpServletRequest request,SysAutho queryObj,Integer roleId) throws Exception {
    	List<SysAutho> ret = new ArrayList<SysAutho>();
        List<ListBean> result =sysauthoService.queryAuthoForSet( queryObj );
        ret = sysroleService.getRoleAuthos(roleId);
        
        for(ListBean eb : result){
        	for(SysAutho tmp:ret){
        		if(eb.getSeqId().equals(tmp.getSeqId())){
        			eb.setChecked(true);
        			break;
        		}
        	}
        }
        return result;
    }
    
    
    /**
     * 获取前端数据结构列表(丛库)
     * @param request
     * @param queryObj
     * @param roleId
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping( value = "readWebList2", method = RequestMethod.GET )
    public List<ListBean> readWebList2(HttpServletRequest request,SysAutho queryObj,Integer roleId) throws Exception {
    	List<SysAutho> ret = new ArrayList<SysAutho>();
        List<ListBean> result =sysauthoService.queryAuthoForSet2( queryObj );
        ret = sysroleService.getRoleAuthos(roleId);
        
        for(ListBean eb : result){
        	for(SysAutho tmp:ret){
        		if(eb.getSeqId().equals(tmp.getSeqId())){
        			eb.setChecked(true);
        			break;
        		}
        	}
        }
        return result;
    }
    
  
	

}
